package com.cetc.exper;

import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @Classname IOExperTest
 * @Description 节点流/缓冲流测试题
 * @Date 2022/11/24 16:13
 * @Created by louzhuangxi
 */
public class IOExperTest {
    /**
     * 实现图片加密操作
     */
    @Test
    public void test01() {
        File sourceFile = new File("data/1.png");
        File targetFile = new File("data/3.png");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            FileInputStream fis = new FileInputStream(sourceFile);
            FileOutputStream fos = new FileOutputStream(targetFile);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                for (int i = 0; i < len; i++) {
                    bytes[i] = (byte) (bytes[i] ^ 5);
                }
                bos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 实现图片解密操作
     */
    @Test
    public void test02() {
        File sourceFile = new File("data/3.png");
        File targetFile = new File("data/4.png");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            FileInputStream fis = new FileInputStream(sourceFile);
            FileOutputStream fos = new FileOutputStream(targetFile);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                for (int i = 0; i < len; i++) {
                    bytes[i] = (byte) (bytes[i] ^ 5);
                }
                bos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取文本上每个字符出现的次数
     */
    @Test
    public void test03() {
        File sourceFile = new File("data/hello.txt");
        File targetFile = new File("data/hello7.txt");
        BufferedReader br = null;
        BufferedWriter bw = null;
        Map<Character, Integer> characterMap = new HashMap<>();
        try {
            FileReader fr = new FileReader(sourceFile);
            FileWriter fw = new FileWriter(targetFile);
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);
            int len;
            while ((len = br.read()) != -1) {
                char data = (char) len;
                if (!characterMap.containsKey(data)) {
                    characterMap.put(data, 1);
                } else {
                    characterMap.put(data, characterMap.get(data) + 1);
                }
            }

            for (Map.Entry<Character, Integer> entry : characterMap.entrySet()) {
                switch (entry.getKey()) {
                    case ' ':
                        bw.write("空格出现了" + entry.getValue() + "次");
                        break;
                    case '\t':
                        bw.write("Tab出现了" + entry.getValue() + "次");
                        break;
                    case '\r':
                        bw.write("回车出现了" + entry.getValue() + "次");
                        break;
                    case '\n':
                        bw.write("换行出现了" + entry.getValue() + "次");
                        break;
                    default:
                        bw.write(entry.getKey() + "出现了" + entry.getValue() + "次");
                }
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test04() {
        //启动临时文件定时删除任务
        File file = new File("/Users/louzhuangxi/Desktop/temp");
        //计算当前时间与文件最后修改时间的时间间隔是否大于1小时,如果是,则主动删除临时文件
        recursionFile(file);
    }

    public void recursionFile(File file) {
        Optional<File> optionalFile = Optional.ofNullable(file);
        optionalFile.ifPresent(f -> {
            for (File listFile : f.listFiles()) {
                if (listFile.isDirectory()) {
                    recursionFile(listFile);
                }
                if (System.currentTimeMillis() - listFile.lastModified() > 1 * 60 * 1000) {
                    listFile.delete();
                }
            }
        });
    }


}
